#include"NetCal.hpp"
#include"Protocol.hpp"
#include "TcpServer.hpp"
#include<memory>
#include"Daemon.hpp"

void Usage(std::string proc)
{
    std::cerr<<"Usage: "<<proc<<"server_ip server_port"<<std::endl;
}

int main(int argc,char *argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        exit(USEAGE_ERR);
    }
    std::cout<<"服务器已经启动，已经是一个守护进程了"<<std::endl;
    //daemon(1,1);
    Daemon(0,0);

    Enable_File_Log_Strategy();

    //1.顶层
    std::unique_ptr<NetCal> cal = std::make_unique<NetCal>();

    //2.协议层
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](Request &req){
        return cal->Execute(req);
    });

    //3.服务器层
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]),[&protocol](std::shared_ptr<Socket>&sock,InetAddr &client){
        protocol->GetRequest(sock,client);
    });
    tsvr->Start();
    return 0;
}